package com.oracle.lingshangejia.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseDialogFragment;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.bean.DoctorVideoBean;
import com.oracle.lingshangejia.bean.ExpertVideoTitleBean;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @松 on 2019/6/10 17:26
 * 选择专家视频对话框
 */
public class DoctorVideoDialog extends BaseDialogFragment {


    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    private Listener mListener;
    private List<String> mOffice = new ArrayList<>(); //科室名字列表
    private List<String> mDoctor = new ArrayList<>();//专家列表
    @BindView(R.id.office)
    Spinner mSpinnerOffice;
    private int mTitleId;
    private ExpertVideoTitleBean mExpertVideoTitleBean;
    private DoctorVideoBean mDoctorVideoBean;
    @BindView(R.id.three_doctor)
    Spinner mSpinner;
    private int mDoctorId  = 0;//专家id
    private DoctorVideoBean.DataBean mDataBean; //专家数据类

    @Override
    public int setLayoutId() {
        return R.layout.dialog_doctor_video;
    }

    @Override
    protected void initWindow(Window window) {
        setLayoutParams(window, Gravity.CENTER,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,
                0, 0);

    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        ButterKnife.bind(this, view);
        getNetTitleList();

    }



    public void setListener(Listener listener){
        mListener = listener;

    }

    public interface Listener{
        void select(String id);
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                 if (mDoctorId ==0){
                     MyToast.show("请选择专家");
                 }else {
                     addSpecialist();
                 }
                break;
        }
    }
    //添加选择专家
    private void addSpecialist(){
        HashMap<Object,Object> map = new HashMap<>();
        Bundle bundle = getArguments();

        map.put("zrysId",mDoctorId );
        map.put("jzId",bundle.getInt("jzid") );
        OkHttpUtils.getNetWork(getContext(), Constant.addSpecialist,map,specialist);
    }

    @SuppressLint("HandlerLeak")
    private android.os.Handler specialist=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    DataBean bean = gson.fromJson(s,DataBean.class );
                    if (bean.getCode() == 0){
                        mListener.select(mDataBean.getPhone());
                        dismiss();
                    }
                    /*mExpertVideoTitleBean = gson.fromJson(s,ExpertVideoTitleBean.class);
                    if(mExpertVideoTitleBean.getCode()==0){
                        setOffice(mExpertVideoTitleBean);
                        mTitleId = mExpertVideoTitleBean.getData().get(0).getId();
                        getNetDoctorList(mTitleId);

                    }else{
                        Toast.makeText(getContext(), mExpertVideoTitleBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }*/

                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(getContext(),error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getContext(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getContext(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    /**
     * 获取科室列表
     */
    private void getNetTitleList() {
        HashMap<Object,Object> map = new HashMap<>();
        OkHttpUtils.getNetWork(getContext(), Constant.baseurl+"online/ksList",map,handler);
    }

    /**
     * 获取科室列表handler
     */
    @SuppressLint("HandlerLeak")
    private android.os.Handler handler=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    mExpertVideoTitleBean = gson.fromJson(s,ExpertVideoTitleBean.class);
                    if(mExpertVideoTitleBean.getCode()==0){
                        setOffice(mExpertVideoTitleBean);
                        mTitleId = mExpertVideoTitleBean.getData().get(0).getId();
                        getNetDoctorList(mTitleId);

                    }else{
                        Toast.makeText(getContext(), mExpertVideoTitleBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }

                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(getContext(),error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getContext(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getContext(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    //设置科室数据
    private void setOffice(ExpertVideoTitleBean titleBean){
        for (ExpertVideoTitleBean.DataBean dataBean :titleBean.getData()){
            mOffice.add(dataBean.getName());

        }
        mSpinnerOffice.setAdapter(new ArrayAdapter<>(getContext(),android.R.layout.simple_spinner_dropdown_item,mOffice));
        mSpinnerOffice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (mOffice.size()>0){
                    mTitleId = mExpertVideoTitleBean.getData().get(position).getId();
                    getNetDoctorList(mTitleId);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void getNetDoctorList(int ksId){
        int ysid= (int) SpUtils.get(getContext(),"ysid",0);
        HashMap<Object,Object> map = new HashMap<>();
        map.put("ksId",ksId);
        map.put("userId",ysid);//当前登录用户id
        OkHttpUtils.getNetWork(getContext(), Constant.baseurl+"online/sjysList",map,doctor);
    }

    //设置专家列表
    private void setDoctorVideoList(DoctorVideoBean doctorVideoBean) {
        mDoctor.clear();
        for (DoctorVideoBean.DataBean dataBean:doctorVideoBean.getData()){
            mDoctor.add(dataBean.getYsmc());
        }
        mDoctorId =doctorVideoBean.getData().get(0).getId();
        mDataBean = doctorVideoBean.getData().get(0);
        mSpinner.setAdapter(new ArrayAdapter<>(getContext(),android.R.layout.simple_spinner_dropdown_item,mDoctor));
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mDoctorId = mDoctorVideoBean.getData().get(position).getId();
                mDataBean = mDoctorVideoBean.getData().get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



    }

    /**
     * 医生handler
     */
    private Handler doctor=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    mDoctorVideoBean = gson.fromJson(s,DoctorVideoBean.class);
                    if (mDoctorVideoBean.getCode()==0) {
                        setDoctorVideoList(mDoctorVideoBean);
                    }else{
                        Toast.makeText(getContext(), mDoctorVideoBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }

                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(getContext(),error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getContext(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getContext(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
